package com.yonyou.common.git;

import com.intellij.notification.NotificationType;
import com.intellij.openapi.progress.ProgressIndicator;
import com.intellij.openapi.progress.ProgressManager;
import com.intellij.openapi.progress.Task;
import com.intellij.openapi.ui.MessageType;
import com.intellij.openapi.ui.Messages;
import com.intellij.openapi.ui.popup.Balloon;
import com.intellij.openapi.ui.popup.JBPopupFactory;
import com.intellij.openapi.wm.StatusBar;
import com.intellij.openapi.wm.WindowManager;
import com.intellij.ui.awt.RelativePoint;
import com.intellij.vcs.log.Hash;
import com.yonyou.biz.vo.GitLogContext;
import com.yonyou.demo.PagePanel;
import com.yonyou.demo.UserListPanel;
import com.yonyou.demo.UserSearch;
import git4idea.GitLocalBranch;
import git4idea.branch.GitBranchUtil;
import git4idea.commands.*;
import git4idea.repo.GitRemote;
import git4idea.repo.GitRepository;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.gitlab4j.api.*;
import org.gitlab4j.api.models.Commit;
import org.gitlab4j.api.models.Issue;
import org.gitlab4j.api.models.IssueFilter;
import org.gitlab4j.api.models.Project;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

public class GiLogQueryProcess extends Task.Backgroundable {

    private GitLogContext context;
    private UserListPanel userListPanel;
    private GitRepository currentRepository;
    public GiLogQueryProcess(GitLogContext context,UserListPanel userListPanel, GitRepository currentRepository) {
        super(context.getProject(), "提交记录查询");
        this.userListPanel = userListPanel;
        this.context = context;
        this.currentRepository = currentRepository;
    }

    @Override
    public void run(@NotNull ProgressIndicator progressIndicator) {
        // 设置进度条为模糊进度条
        progressIndicator.setIndeterminate(true);
        // 这里就是我们需要模拟执行我们相关的业务逻辑的部分，此处就使线程睡眠3秒作为模拟
        try {
            userListPanel.getSearch().setRoleId(null);
            userListPanel.getSearch().setKey(userListPanel.getKeyField().getText());
            queryList(userListPanel.getGit_token().getText());
            userListPanel.getP().setPage(1);
            userListPanel.getModel().fireTableDataChanged();
        } catch (Exception ex) {
            com.yonyou.ctm.ctmplugin.utils.NotificationUtil.showNotification(NotificationType.ERROR,ex.getMessage());
        }

        // 开启进度条
        progressIndicator.setIndeterminate(true);
        // 设置进度条为百分百
        progressIndicator.setFraction(1.0);

        progressIndicator.setText("查询结果条数："+userListPanel.getList().size());
        // 在状态栏提示信息【代码生成完成】
        StatusBar statusBar = WindowManager.getInstance().getStatusBar(this.context.getProject());
        JBPopupFactory.getInstance()
                .createHtmlTextBalloonBuilder("查询结果条数："+userListPanel.getList().size(), MessageType.INFO, null)
                .setFadeoutTime(6000)
                .createBalloon()
                .show(RelativePoint.getCenterOf(statusBar.getComponent()), Balloon.Position.atRight);

    }
    public void queryList(String gittoekn) throws Exception {

        Collection<GitRemote> remotes = currentRepository.getRemotes();
        String remoteUrl = remotes.iterator().next().getUrls().get(0);
        GitLabApi gitLabApi = new GitLabApi(remoteUrl.substring(0,remoteUrl.indexOf("com")+4),this.userListPanel.getGit_token().getText());
        this.context.setGitLabApi(gitLabApi);

        // 获取项目列表
        // 获取CommitsApi实例
        userListPanel.getList().clear();
        List<Commit> temp_list = gitLabApi.getCommitsApi().getCommits(userListPanel.getGitProject(gitLabApi,currentRepository).getId(),currentRepository.getCurrentBranchName(), null,null, null);
        if(StringUtils.isEmpty(userListPanel.getSearch().getKey())){
            userListPanel.getList().addAll(temp_list);
        }else{
            for(Commit c :temp_list){
                if(c.getMessage().contains(userListPanel.getSearch().getKey())){
                    userListPanel.getList().add(c);
                }
            }
        }

    }

}
